package com.webserver.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * 用于处理客户端的交互
 */
public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }
    public void run(){
        try {
            InputStream in = socket.getInputStream();;
            int d;
            char pre = 'a';//记录上次读取的字符内容
            char cur = 'a';//记录本次读取的字符内容
            StringBuilder builder = new StringBuilder();//记录拼接后的字符串内容
            while((d = in.read())!=-1) {
                cur = (char) d;//本次读取到的字符
                if(pre==13&&cur==10){
                    break;
                }
                builder.append(cur);
                pre = cur;//将本次读取的字符在下次循环是记录为上次读取的字符
            }
            String line = builder.toString().trim();
            System.out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
        }
    }
}
